package com.itfreer.power.service.user;

import java.io.IOException;
import java.util.HashMap;

import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseRestService;
import com.itfreer.power.entity.user.UserInfoEntity;
import com.itfreer.power.entity.user.VUserInfoEntity;
import com.itfreer.utils.json.JsonUtils;

/**
 * 定义用户服务
 */
@Component
@Path("/userinfo/")
public class RestUserInfoService {
	
	/**
	 * 用户信息管理接口
	 */
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 获取实体
	 * @param guid			唯一值
	 * @return
	 */
	@POST
	@Path("/getEntity/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public UserInfoEntity getEntity(@PathParam("id") String guid){
		return userInfoService.getEntity(guid);
	}
	
	/**
	 * 添加实体
	 * @param entity		添加实体
	 * @return
	 * @throws IOException 
	 */
	@POST
	@Path("/add")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public VUserInfoEntity add(String data) throws IOException{
		HashMap<String, Object> args = BaseRestService.getRestParam(data);
		String entityJson = null;
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		UserInfoEntity entity = (UserInfoEntity) JsonUtils.toBean(entityJson, UserInfoEntity.class);
		return userInfoService.add(entity);
	}
	
	/**
	 * 更新实体
	 * @param entity		实体
	 * @return
	 * @throws IOException 
	 */
	@POST
	@Path("/update")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public VUserInfoEntity update(String data) throws IOException{
		HashMap<String, Object> args = BaseRestService.getRestParam(data);
		String entityJson = null;
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		UserInfoEntity entity = (UserInfoEntity) JsonUtils.toBean(entityJson, UserInfoEntity.class);
		return userInfoService.update(entity);
	}
	
	/**
	 * 删除实体
	 * @param guid			唯一值
	 */
	@DELETE
	@Path("/delete/{id:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public void delete(@PathParam("id") String guid){
		userInfoService.delete(guid);
	}
	
	/**
	 * 指量删除实体
	 * @param guid			唯一值集
	 */
	@DELETE
	@Path("/deletes/{ids:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public void deletes(@PathParam("ids") String guids){
		userInfoService.deletes(guids);
	}
}